' This script file was created from other bits of scripts on the web

CONST ADS_UF_ACCOUNT_TRUSTED = &H80000
Const GENERAL_WARNING = 1
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1


Set ArgObj = WScript.Arguments
If ArgObj.Count < 2 Then
	DisplayHelpMessage()
	WScript.Quit (GENERAL_FAILURE)
End If

user = ArgObj.Item(0)
enable = ArgObj.Item(1)

if ((enable <> "enable") and (enable <> "disable") ) Then
	DisplayHelpMessage()
	WScript.Quit (GENERAL_FAILURE)
end if

DIM objUser
'<<<< Bind to the user object using the distinguished name >>>>
' SET objUser = GETOBJECT(user)
SET objUser = GetUserObject(user)

intUAC = objUser.GET("userAccountControl")

if (enable = "enable") then
	WScript.Echo "enabling..."
	IF  (intUAC AND ADS_UF_ACCOUNT_TRUSTED)=0 THEN
		Toggle()
		WScript.Echo "enabled"
	else
		WScript.Echo "All ready enabled"
		WScript.Quit 
	end if
else
	WScript.Echo "disabling..."
	IF intUAC AND ADS_UF_ACCOUNT_TRUSTED THEN
		Toggle()
		WScript.Echo "disabled"
	else
		WScript.Echo "All ready disabled"
		WScript.Quit 
	end if
end if

'<<<<< Disable Account is trusted for delegation >>>>>
IF intUAC AND ADS_UF_ACCOUNT_TRUSTED THEN
	objUser.put "userAccountControl",  intUAC XOR ADS_UF_ACCOUNT_TRUSTED
	objUser.setinfo
END IF

Sub DisplayHelpMessage()
    WScript.Echo
    WScript.Echo "Usage:"
    WScript.Echo "  userdelegation.VBS ""<ldap path>"" <enable/disable>"
    WScript.Echo "  ldap example : ""LDAP://cn=Wisdom V6,cn=users,dc=diagonal-solutions,dc=local"""
    WScript.Echo
end sub

sub Toggle()
	objUser.put "userAccountControl",  intUAC XOR ADS_UF_ACCOUNT_TRUSTED
	objUser.setinfo
end sub

Public Function GetUserObject(user)
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("defaultNamingContext")
    Set objTrans = CreateObject("NameTranslate")
    objTrans.Init ADS_NAME_INITTYPE_GC, ""
    objTrans.set ADS_NAME_TYPE_1779, strDNSDomain
    objTrans.set ADS_NAME_TYPE_NT4, user
    strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

    Set GetUserObject = GetObject("LDAP://" + strUserDN)
End Function
